package cc.kigh.user.controller;

import java.util.List;
import java.util.Arrays;

import cc.kigh.common.annotation.Log;
import cc.kigh.common.annotation.RepeatSubmit;
import cc.kigh.common.core.controller.BaseController;
import cc.kigh.common.core.domain.AjaxResult;
import cc.kigh.common.core.domain.PageQuery;
import cc.kigh.common.core.page.TableDataInfo;
import cc.kigh.common.core.validate.AddGroup;
import cc.kigh.common.core.validate.EditGroup;
import cc.kigh.common.core.validate.QueryGroup;
import cc.kigh.common.enums.BusinessType;
import cc.kigh.common.utils.poi.ExcelUtil;
import cc.kigh.user.domain.bo.UmsUserBo;
import cc.kigh.user.domain.vo.UmsUserVo;
import cc.kigh.user.service.IUmsUserService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 用户Controller
 *
 * @author ruoyi
 * @date 2022-02-17
 */
@Validated
@Api(value = "用户控制器", tags = {"用户管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/ums/user")
public class UmsUserController extends BaseController {

    private final IUmsUserService iUmsUserService;

    /**
     * 查询用户列表
     */
    @ApiOperation("查询用户列表")
    @PreAuthorize("@ss.hasPermi('hosp:user:list')")
    @GetMapping("/list")
    public TableDataInfo<UmsUserVo> list(@Validated(QueryGroup.class) UmsUserBo bo, PageQuery pageQuery) {
        return iUmsUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户列表
     */
    @ApiOperation("导出用户列表")
    @PreAuthorize("@ss.hasPermi('hosp:user:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated UmsUserBo bo, HttpServletResponse response) {
        List<UmsUserVo> list = iUmsUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户", UmsUserVo.class, response);
    }

    /**
     * 获取用户详细信息
     */
    @ApiOperation("获取用户详细信息")
    @PreAuthorize("@ss.hasPermi('hosp:user:query')")
    @GetMapping("/{cid}")
    public AjaxResult<UmsUserVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("cid") Long cid) {
        return AjaxResult.success(iUmsUserService.queryById(cid));
    }

    /**
     * 新增用户
     */
    @ApiOperation("新增用户")
    @PreAuthorize("@ss.hasPermi('hosp:user:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody UmsUserBo bo) {
        return toAjax(iUmsUserService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改用户
     */
    @ApiOperation("修改用户")
    @PreAuthorize("@ss.hasPermi('hosp:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody UmsUserBo bo) {
        return toAjax(iUmsUserService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除用户
     */
    @ApiOperation("删除用户")
    @PreAuthorize("@ss.hasPermi('hosp:user:remove')")
    @Log(title = "用户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{cids}")
    public AjaxResult<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] cids) {
        return toAjax(iUmsUserService.deleteWithValidByIds(Arrays.asList(cids), true) ? 1 : 0);
    }
}
